//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "PerformanceOptionPathPricer.h"
using namespace Cephei::QL::Pricingengines::Cliquet;
#include <gen/QL/Methods/Montecarlo/Path.h>
using namespace Cephei::QL::Methods::Montecarlo;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Cliquet::CPerformanceOptionPathPricer::CPerformanceOptionPathPricer (QL::Option::TypeEnum type, Double strike, Cephei::IVector<Double>^ discounts) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CoVector<Double>^ _Cdiscounts;
    try
    {
#ifdef HANDLE
        _phPerformanceOptionPathPricer = NULL;
#endif
        QuantLib::Option::Type _type = (QuantLib::Option::Type)type ;
        QuantLib::Real _strike = (QuantLib::Real)ValueHelper::Convert (strike);
        CoVector<Double>^ _Cdiscounts = safe_cast<CoVector<Double>^> (discounts);
        _Cdiscounts->Lock();
        INativeVector<Double>^ _NCIdiscounts = _Cdiscounts->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCdiscounts = safe_cast<CDoubleVector^>(_NCIdiscounts);
        std::vector<QuantLib::DiscountFactor>& _discounts = static_cast<std::vector<QuantLib::DiscountFactor>&> (_NCdiscounts->GetReference ());
        _ppPerformanceOptionPathPricer = new boost::shared_ptr<QuantLib::PerformanceOptionPathPricer> (new QuantLib::PerformanceOptionPathPricer ( _type,  _strike,  _discounts ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdiscounts != nullptr) _Cdiscounts->Unlock();
    }
}
Cephei::QL::Pricingengines::Cliquet::CPerformanceOptionPathPricer::CPerformanceOptionPathPricer (boost::shared_ptr<QuantLib::PerformanceOptionPathPricer>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPerformanceOptionPathPricer = NULL;
#endif
	_ppPerformanceOptionPathPricer = &childNative;
    
}
Cephei::QL::Pricingengines::Cliquet::CPerformanceOptionPathPricer::CPerformanceOptionPathPricer (QuantLib::PerformanceOptionPathPricer& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phPerformanceOptionPathPricer = NULL;
#endif
	_ppPerformanceOptionPathPricer = new boost::shared_ptr<QuantLib::PerformanceOptionPathPricer> (&childNative);
    
    _PerformanceOptionPathPricerOwner = owner;
    
}

Cephei::QL::Pricingengines::Cliquet::CPerformanceOptionPathPricer::CPerformanceOptionPathPricer (CPerformanceOptionPathPricer^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPerformanceOptionPathPricer = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppPerformanceOptionPathPricer = new boost::shared_ptr<QuantLib::PerformanceOptionPathPricer> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Cliquet::CPerformanceOptionPathPricer::CPerformanceOptionPathPricer (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPerformanceOptionPathPricer = NULL;
#endif
	if (!t->IsSubclassOf(CPerformanceOptionPathPricer::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Cliquet::CPerformanceOptionPathPricer::CPerformanceOptionPathPricer (QuantLib::Handle<QuantLib::PerformanceOptionPathPricer>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPerformanceOptionPathPricer = &childNative;
	_ppPerformanceOptionPathPricer = &static_cast<boost::shared_ptr<QuantLib::PerformanceOptionPathPricer>>(childNative.currentLink());
    
    _PerformanceOptionPathPricerOwner = owner;
}
Cephei::QL::Pricingengines::Cliquet::CPerformanceOptionPathPricer::CPerformanceOptionPathPricer (QuantLib::Handle<QuantLib::PerformanceOptionPathPricer> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phPerformanceOptionPathPricer = &childNative;
	_ppPerformanceOptionPathPricer = &static_cast<boost::shared_ptr<QuantLib::PerformanceOptionPathPricer>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Cliquet::CPerformanceOptionPathPricer::CPerformanceOptionPathPricer (QuantLib::PerformanceOptionPathPricer childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phPerformanceOptionPathPricer = NULL;
#endif
	_ppPerformanceOptionPathPricer = new boost::shared_ptr<QuantLib::PerformanceOptionPathPricer> (new QuantLib::PerformanceOptionPathPricer (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Cliquet::CPerformanceOptionPathPricer::~CPerformanceOptionPathPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppPerformanceOptionPathPricer != NULL)
    {
	    delete _ppPerformanceOptionPathPricer;
        _ppPerformanceOptionPathPricer = NULL;
    }
}
Cephei::QL::Pricingengines::Cliquet::CPerformanceOptionPathPricer::!CPerformanceOptionPathPricer ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppPerformanceOptionPathPricer != NULL)
    {
	    delete _ppPerformanceOptionPathPricer;
    }
}
QuantLib::PerformanceOptionPathPricer& Cephei::QL::Pricingengines::Cliquet::CPerformanceOptionPathPricer::GetReference ()
{
    if (_ppPerformanceOptionPathPricer == NULL) throw gcnew NativeNullException ();
	return **_ppPerformanceOptionPathPricer;
}
boost::shared_ptr<QuantLib::PerformanceOptionPathPricer>& Cephei::QL::Pricingengines::Cliquet::CPerformanceOptionPathPricer::GetShared ()
{
    if (_ppPerformanceOptionPathPricer == NULL) throw gcnew NativeNullException ();
	return *_ppPerformanceOptionPathPricer;
}
QuantLib::PerformanceOptionPathPricer* Cephei::QL::Pricingengines::Cliquet::CPerformanceOptionPathPricer::GetPointer ()
{
    if (_ppPerformanceOptionPathPricer == NULL) throw gcnew NativeNullException ();
	return &**_ppPerformanceOptionPathPricer;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::PerformanceOptionPathPricer>& Cephei::QL::Pricingengines::Cliquet::CPerformanceOptionPathPricer::GetHandle ()
{
	if (_phPerformanceOptionPathPricer == NULL)
	{
		_phPerformanceOptionPathPricer = new Handle<QuantLib::PerformanceOptionPathPricer> (*_ppPerformanceOptionPathPricer);
	}
	return *_phPerformanceOptionPathPricer;
}
#endif
bool Cephei::QL::Pricingengines::Cliquet::CPerformanceOptionPathPricer::HasNative () 
{
	return (_ppPerformanceOptionPathPricer != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Cliquet::IPerformanceOptionPathPricer^ Cephei::QL::Pricingengines::Cliquet::CPerformanceOptionPathPricer_Factory::Create (QL::Option::TypeEnum type, Double strike, Cephei::IVector<Double>^ discounts)
{
    return gcnew CPerformanceOptionPathPricer ( type,  strike,  discounts);
}
